package nl.hanze.monoalphabetic.ui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import nl.hanze.monoalphabetic.controller.MainController;

/**
 *
 * @author alex
 */
public class EncryptionPanel extends JPanel {

    private MainController mainController;
    private JTextPane txtOutput, txtInput;
    List<JLabel> labels;
    List<JTextField> textFields;
    private final Font labelFont;

    public EncryptionPanel(MainController mainController) {
        this.mainController = mainController;
        mainController.generateConversionTable();
        labels = new ArrayList<>();
        textFields = new ArrayList<>();
        labelFont = new Font("Courier new", Font.BOLD, 14);

        setLayout(null);

        addInputPanel();
        addOutputPanel();
        addMappingPanel();
        setVisible(true);

    }

    public void addInputPanel() {
        txtInput = new JTextPane();
        JScrollPane scrollPane = new JScrollPane(txtInput);
        scrollPane.setBounds(20, 10, 600, 200);
        add(scrollPane);

        txtInput.addKeyListener(new KeyAdapter() {

            @Override
            public void keyReleased(KeyEvent e) {
                txtOutput.setText(mainController.scrambleText(txtInput.getText()));
                txtOutput.setCaretPosition(0);
            }
        });

        JButton butCodeer = new JButton("Codeer text");
        butCodeer.setBounds(20, 220, 100, 25);
        add(butCodeer);

        butCodeer.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //mainController.generateConversionTable();
                txtOutput.setText(mainController.scrambleText(txtInput.getText()));
                txtOutput.setCaretPosition(0);
            }
        });
    }

    public void addOutputPanel() {
        txtOutput = new JTextPane();
        txtOutput.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(txtOutput);
        scrollPane.setBounds(20, 280, 600, 200);
        add(scrollPane);
    }

    public void addMappingPanel() {
        JLabel labMapping = new JLabel("Alfabet mapping");
        labMapping.setBounds(700, 10, 100, 20);
        add(labMapping);

        JButton butRefresh = new JButton("Genereer");
        butRefresh.setBounds(800, 10, 100, 20);
        butRefresh.setToolTipText("Genereer een nieuwe conversietabel");
        add(butRefresh);

        butRefresh.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                mainController.generateConversionTable();
                txtOutput.setText(mainController.scrambleText(txtInput.getText()));
                updateLabels();
            }
        });

        createLabels();
    }

    /**
     * Creëer labels en zet ze in ArrayList
     */
    public void createLabels() {
        int hDistance = 25;
        int vDistance = 20;

        for (int i = 0; i < 26; i++) {
            JLabel label = new JLabel(((char) ('a' + i)) + " => " + mainController.getConversion().get((char) ('a' + i)));
            label.setBounds(700, i * vDistance + 30, 150, 20);
            label.setFont(labelFont);
            add(label);
            labels.add(label);
        }
    }

    /**
     *Werk labels bij
     */
    public void updateLabels() {
        for (int i = 0; i < 26; i++) {
            JLabel label = labels.get(i);
            label.setText(((char) ('a' + i)) + " => " + mainController.getConversion().get((char) ('a' + i)));
        }
    }
    
    
    
}
